/*
 * @Author: taoyuqing
 * @Date: 2022-12-28 22:47:55
 * @Description: 
 */
import 'package:flutter/material.dart';

import '../../utils/utils.dart';

class GridImgs extends StatelessWidget {
  final List games;
  final Function? ontap;
  const GridImgs({Key? key, required this.games, this.ontap}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Wrap(
      spacing: 15,
      runSpacing: 15,
      children: _getImagesInfo(context),
    );
  }

  List<Widget> _getImagesInfo(BuildContext context) {
    double imgW = (Utils().getScreenWidth(context) - 70) / 3;
    return games.asMap().keys.map((i) {
      Image image =
          Image.asset(games[i] is String ? games[i] : games[i]['img']);
      FutureBuilder fb = FutureBuilder(
          future: Utils().getLocalImageSize(
            img: image,
          ),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            dynamic info = snapshot.data;
            if (snapshot.connectionState == ConnectionState.done) {
              if (info.width > info.height) {
                Image img = Image.asset(
                  games[i] is String ? games[i] : games[i]['img'],
                  width: imgW * 2 + 15,
                );
                return GestureDetector(
                onTap: () {
                  if (ontap != null) {
                    ontap!(games[i]);
                  }
                },
                child: img,
              );
              }
              Image img = Image.asset(
                games[i] is String ? games[i] : games[i]['img'],
                width: imgW,
              );
              return GestureDetector(
                onTap: () {
                  if (ontap != null) {
                    ontap!(games[i]);
                  }
                },
                child: img,
              );
            }
            return CircularProgressIndicator();
          });
      return fb;
    }).toList();
  }
}
